package com.shide.common.util;

import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BankNumUtils {


    @Data
    @Builder
    public static class BankInfo {
        private String bankName;

        private String bankCode;

        private List<String> numberPatterns;
    }

    public static List<BankInfo> bankList = new ArrayList<>();

    static {
        BankInfo PSBC = BankInfo.builder()
                .bankName("中国邮政储蓄银行")
                .bankCode("PSBC")
                .numberPatterns(Arrays.asList(
                        "^(621096|621098|622150|622151|622181|622188|622199|955100|621095|620062|621285|621798|621799|621797|620529|621622|621599|621674|623218|623219)\\d{13}$",
                        "^(62215049|62215050|62215051|62218850|62218851|62218849)\\d{11}$")
                )
                .build();
        BankInfo CMB = BankInfo.builder()
                .bankName("招商银行")
                .bankCode("CMB")
                .numberPatterns(Arrays.asList(
                        "^(402658|410062|468203|512425|524011|622580|622588|622598|622609|95555|621286|621483|621485|621486|621299)\\d{10}$",
                        "^(62215049|62215050|62215051|62218850|62218851|62218849)\\d{11}$",
                        "^(690755)\\d{12}$")
                )
                .build();
        BankInfo COMM = BankInfo.builder()
                .bankName("中国交通银行")
                .bankCode("COMM")
                .numberPatterns(Arrays.asList(
                        "^(622261|622260|622262|621002|621069|621436|621335)\\d{13}$",
                        "^(620013)\\d{10}$",
                        "^(405512|601428|405512|601428|622258|622259|405512|601428)\\d{11}$")
                )
                .build();
        BankInfo CCB = BankInfo.builder()
                .bankName("中国建设银行")
                .bankCode("CCB")
                .numberPatterns(Arrays.asList(
                        "^(621284|436742|589970|620060|621081|621467|621598|621621|621700|622280|622700|623211|623668)\\d{13}$",
                        "^(421349|434061|434062|524094|526410|552245|621080|621082|621466|621488|621499|622966|622988|622382|621487|621083|621084|620107)\\d{10}$",
                        "^(436742193|622280193)\\d{10}$"
                ))
                .build();
        BankInfo BOC = BankInfo.builder()
                .bankName("中国银行")
                .bankCode("BOC")
                .numberPatterns(Arrays.asList(
                        "^(621660|621661|621662|621663|621665|621667|621668|621669|621666|456351|601382|621256|621212|621283|620061|621725|621330|621331|621332|621333|621297|621568|621569|621672|623208|621620|621756|621757|621758|621759|621785|621786|621787|621788|621789|621790|622273|622274|622771|622772|622770|621741|621041)\\d{13}$",
                        "^(621293|621294|621342|621343|621364|621394|621648|621248|621215|621249|621231|621638|621334|621395|623040|622348)\\d{10}$"
                ))
                .build();
        BankInfo ABC = BankInfo.builder()
                .bankName("中国农业银行")
                .bankCode("ABC")
                .numberPatterns(Arrays.asList(
                        "^(622841|622824|622826|622848|620059|621282|622828|622823|621336|621619|622821|622822|622825|622827|622845|622849|623018|623206|621671|622840|622843|622844|622846|622847|620501)\\d{13}$",
                        "^(95595|95596|95597|95598|95599)\\d{14}$",
                        "^(103)\\d{16}$"
                ))
                .build();
        BankInfo ICBC = BankInfo.builder()
                .bankName("中国工商银行")
                .bankCode("ICBC")
                .numberPatterns(Arrays.asList(
                        "^(620200|620302|620402|620403|620404|620406|620407|620409|620410|620411|620412|620502|620503|620405|620408|620512|620602|620604|620607|620611|620612|620704|620706|620707|620708|620709|620710|620609|620712|620713|620714|620802|620711|620904|620905|621001|620902|621103|621105|621106|621107|621102|621203|621204|621205|621206|621207|621208|621209|621210|621302|621303|621202|621305|621306|621307|621309|621311|621313|621211|621315|621304|621402|621404|621405|621406|621407|621408|621409|621410|621502|621317|621511|621602|621603|621604|621605|621608|621609|621610|621611|621612|621613|621614|621615|621616|621617|621607|621606|621804|621807|621813|621814|621817|621901|621904|621905|621906|621907|621908|621909|621910|621911|621912|621913|621915|622002|621903|622004|622005|622006|622007|622008|622010|622011|622012|621914|622015|622016|622003|622018|622019|622020|622102|622103|622104|622105|622013|622111|622114|622017|622110|622303|622304|622305|622306|622307|622308|622309|622314|622315|622317|622302|622402|622403|622404|622313|622504|622505|622509|622513|622517|622502|622604|622605|622606|622510|622703|622715|622806|622902|622903|622706|623002|623006|623008|623011|623012|622904|623015|623100|623202|623301|623400|623500|623602|623803|623901|623014|624100|624200|624301|624402|623700|624000)\\d{12}$",
                        "^(622200|622202|622203|622208|621225|620058|621281|900000|621558|621559|621722|621723|620086|621226|621618|620516|621227|621288|621721|900010|623062|621670|621720|621379|621240|621724|621762|621414|621375|622926|622927|622928|622929|622930|622931|621733|621732|621372|621369|621763)\\d{13}$",
                        "^(402791|427028|427038|548259|621376|621423|621428|621434|621761|621749|621300|621378|622944|622949|621371|621730|621734|621433|621370|621764|621464|621765|621750|621377|621367|621374|621731|621781)\\d{10}$",
                        "^(9558)\\d{15}$"
                ))
                .build();
        BankInfo CEB = BankInfo.builder()
                .bankName("中国光大银行")
                .bankCode("CEB")
                .numberPatterns(Arrays.asList(
                        "^(303)\\d{13}$",
                        "^(90030)\\d{11}$",
                        "^(620535)\\d{13}$",
                        "^(620085|622660|622662|622663|622664|622665|622666|622667|622669|622670|622671|622672|622668|622661|622674|622673|620518|621489|621492)\\d{10}$"
                ))
                .build();
        BankInfo CITIC = BankInfo.builder()
                .bankName("中信银行")
                .bankCode("CITIC")
                .numberPatterns(Arrays.asList(
                        "^(433670|433680|442729|442730|620082|622690|622691|622692|622696|622698|622998|622999|433671|968807|968808|968809|621771|621767|621768|621770|621772|621773|622453|622456)\\d{10}$",
                        "^(622459)\\d{11}$",
                        "^(620535)\\d{13}$",
                        "^(620085|622660|622662|622663|622664|622665|622666|622667|622669|622670|622671|622672|622668|622661|622674|622673|620518|621489|621492)\\d{10}$"
                ))
                .build();
        // 中国银行
        bankList.add(BOC);
        // 招商银行
        bankList.add(CMB);
        //  中国交通银行
        bankList.add(COMM);
        // 中国建设银行
        bankList.add(CCB);
        // 中国邮政储蓄银行
        bankList.add(PSBC);
        // 中国农业银行
        bankList.add(ABC);
        // 中国工商银行
        bankList.add(ICBC);
        // 光大银行
        bankList.add(CEB);
        // 中信
        bankList.add(CITIC);

    }



    public static BankInfo getBankInfoByCardNumb(String cardNum) {
        for (BankInfo bankInfo : bankList) {
            boolean flag = false;
            for (String numberPattern : bankInfo.getNumberPatterns()) {
                if (Regular(cardNum, numberPattern)) {
                    flag = true;
                }
            }
            if (flag) {
                return bankInfo;
            }
        }
        return null;

    }


    /**
     * 匹配是否符合正则表达式pattern 匹配返回true
     *
     * @param str     匹配的字符串
     * @param pattern 匹配模式
     * @return boolean
     */
    private static boolean Regular(String str, String pattern) {
        if (null == str || str.trim().length() <= 0)
            return false;
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(str);
        return m.matches();
    }
}
